package gdut.jmxywz.action;

import gdut.jmxywz.been.Horizontal;
import gdut.jmxywz.been.HorizontalFile;
import gdut.jmxywz.been.Teacher;
import gdut.jmxywz.util.PageModel;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Random;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;

public class HorizontalAction extends BaseAction{
	private  static final String  TEACHER_SESSION_KEY = "teacherSessionKey";
	private Teacher  teacher;
	
	
	private Horizontal horizontal;
	private PageModel pageModel;
	private int horId;
	
	/***************分页*****************/
	private int currentPage=1;
	private int pageSize=5;
	
	/***************检索*****************/
	//private String searchSelect="";
	
	private String like="";
	private String where_search="";
	private String where_value="";
	//private String searchValue="";
	/**************文件上传****************/
	private List<String> files_content;
	private List<File> upload_file;       
	

	private List<String> upload_fileContentType;
	private List<String> upload_fileFileName;
	private String   savePath;
	private List<HorizontalFile> horizontalFile ;
	/**************excel表导出导入****************/
	private File excelFile;
	private String excelFileContentType;
	private boolean checkbox;
	
	
	
	
	
	
	/***************增加*****************/
	 public String add() throws Exception{
		 //System.out.print("88888888888888888888888888");
		 teacher = (Teacher)ActionContext.getContext().getSession().get(TEACHER_SESSION_KEY);
		 horizontal.setTeacherByEditer(teacher);
boolean bool=this.getCommonService().saveObject(horizontal);
    	 
		 if(upload_file!=null)
		
				for(int i=0;i<upload_file.size();i++){//控制上传部分
					String extend_name=getUpload_fileFileName().get(i);
//					System.out.println(extend_name);
					int extend_name_int=extend_name.lastIndexOf(".");//截取扩展名
					extend_name=extend_name.substring(extend_name_int);
								
					java.text.DateFormat df = new java.text.SimpleDateFormat("yyyyMMddHHmmss") ;//生成时间用于重命名文件
				   String datestr = df.format(new java.util.Date()) ;
				   Random rand = new Random();//生成随机数
				   int ram = rand.nextInt(500);
					String ram_str = String.valueOf(ram);
					for(int i_i=0; ram_str.length()<6;i_i++){
						ram_str="0"+ram_str;
					}
					

					
					
					String root = ServletActionContext.getRequest().getRealPath(
							"/accessories/horizontal/");//获得文件在服务器的绝对路径
					
					extend_name=datestr+ram_str+extend_name;//合成文件名    时间+随机数+扩展名
//					System.out.println("----文件名-:---"+extend_name+"--------------");
					 savePath=root+"\\"+extend_name;
					
//						System.out.println("----服务器保存路径-:---"+savePath+"--------------");
					 
					String fn=savePath ;
					FileOutputStream fos=new  FileOutputStream(fn);
					
					InputStream fis=new FileInputStream(upload_file.get(i));
					byte[] buffer=new byte[8192];
					int len=0;
					while((len=fis.read(buffer))>0){
					fos.write(buffer,0,len);
					
					}
//					fos.close();
//					fis.close();
					
					HorizontalFile afiles=new HorizontalFile();
								
					
//					System.out.println();
//					System.out.println("----------第--"+(i+1)+"--文件--------------");
//					System.out.println("----服务器保存路径-:---"+savePath+"--------------");
//					System.out.println("--------文件名:"+upload_fileFileName.get(i)+"--------------");
//					System.out.println("--------文件说明:"+files_content.get(i)+"--------------");
					
					afiles.setHorizontalFileUrl("/accessories/horizontal/"+extend_name);
					afiles.setHorizontalFileName(upload_fileFileName.get(i));
					afiles.setHorizontalFileContext(files_content.get(i));
					afiles.setHorizontal(horizontal);	
		
					//在插入附件表
					boolean b2=this.getCommonService().saveObject(afiles);
			
//			文件上传处理结束
			
	     	}

		 if(bool)
		 return SUCCESS;
		 else
			 return "error";
	 }


	 
	 
	public String show() throws Exception{
		String hql="from Horizontal where auditing != 0";
		 hql = hql +" order by editedTime desc,horId desc,auditing desc";
		pageModel=this.getCommonService().findPage(hql, null, currentPage, pageSize, "HorizontalShow.action","");
		
			return SUCCESS;
	}


	
	public String search() throws Exception {
		// TODO Auto-generated method stub
		String hql="from Horizontal  where auditing != 0";
		//
		
		
		 String[] parem = null;
   	  
   	  String  search_parameter = "";
   	  String en_where_value="";
   	  
   	  
			HttpServletRequest request = ServletActionContext.getRequest();
			where_value=new String(request.getParameter("where_value").getBytes("ISO-8859-1"),"UTF-8");
			//
		   	  String searchValue1=where_value;
		   	  //
			//
     if(!"".equals(where_value)&&where_value!=null){
   		  
   		  en_where_value=java.net.URLEncoder.encode(where_value, "utf-8");//URL编码转换
   		  System.out.println("++++-- "+where_value);
   		  
   		  if (like.equals("like")) {
   			hql = hql + " and " + where_search + " like '%"+ searchValue1 + "%' ";
 				//hql = hql + "  and sc." + where_search + " like '%"+ searchValue1 + "%' ";
 			} else {
 				hql = hql + " and " + where_search + " ='"+ searchValue1 + "' ";
 				//hql = hql + "  and sc." + where_search + " ='"+ searchValue1 + "' ";
 			}
   		  
   	  }
   	  
     hql = hql +" order by editedTime desc,horId desc,auditing desc";
   	  
   	  //查询条件
   	  
   	  search_parameter="&where_search="+ where_search+"&like="+like+"&where_value="+en_where_value;
   	  
   	  pageModel = new PageModel();
   	  
   	  pageModel=this.getCommonService().findPage(hql, parem, currentPage, pageSize, "HorizontalSearch.action",search_parameter);
		
		
		
		
		return SUCCESS;
		
		
	}

	
	
	/***************详细*****************/
	public String detail() throws Exception {
		// TODO Auto-generated method stub
		String hql="from HorizontalFile where HorId="+horId;
		
		horizontalFile=this.getCommonService().findListByHql(hql, null);
		
//		System.out.print("333333333333"+hql+"33333333333333");
		
		horizontal=(Horizontal)this.getCommonService().findByIntId(horId, "gdut.jmxywz.been.Horizontal");
		if(horizontal==null){
			return "error";	
		}
			
		else{
//			horizontal.setInfoReadtimes(horizontal.getInfoReadtimes()+1);没有阅读次数
	    	  
//	    	  this.getCommonService().updateObject(horizontal);
			
			
			return SUCCESS;
		}
	}
	
	
	/***************更新预处理*****************/
	public String preUpdate() throws Exception {
		// TODO Auto-generated method stub
		 teacher = (Teacher)ActionContext.getContext().getSession().get(TEACHER_SESSION_KEY);

		horizontal=(Horizontal)this.getCommonService().findByIntId(horId, "gdut.jmxywz.been.Horizontal");
		horizontal.setTeacherByAssessor(teacher);
		
		String hql2="from HorizontalFile where horId="+horId;
//		System.out.println(hql2);
		horizontalFile=this.getCommonService().findListByHql(hql2, null);
//		String hql="from Info as a where a.infoType=3";
//		scientificInfoUpdateList=new ArrayList();
//		scientificInfoUpdateList=this.getCommonService().findListByHql(hql, null);
//		
//		if(scientificInfoUpdate==null||scientificInfoUpdateList==null)
			if(horizontal==null)
			return "error";
		else
			return SUCCESS;
	}
	
	/***************非管理员更新预处理*****************/
	public String preUpdateSelf() throws Exception {
		 teacher = (Teacher)ActionContext.getContext().getSession().get(TEACHER_SESSION_KEY);

		// TODO Auto-generated method stub
		horizontal=(Horizontal)this.getCommonService().findByIntId(horId, "gdut.jmxywz.been.Horizontal");
		if(!horizontal.getTeacherByEditer().getTeacherId().equals(teacher.getTeacherId())){
			
			System.out.println("没有修改的权限");
			
			return "error";
		}
	
		if(!"0".equals(horizontal.getAuditing())){
	   		  
	   		System.out.println("已通过审核，不能在修改！");
	   		
	   		return "error";
	   	  }
	 
		
		String hql2="from HorizontalFile where horId="+horId;
//		System.out.println(hql2);
		horizontalFile=this.getCommonService().findListByHql(hql2, null);
//		String hql="from Info as a where a.infoType=3";
//		scientificInfoUpdateList=new ArrayList();
//		scientificInfoUpdateList=this.getCommonService().findListByHql(hql, null);
//		
//		if(scientificInfoUpdate==null||scientificInfoUpdateList==null)
			if(horizontal==null)
			return "error";
		else
			return SUCCESS;
	}
	
	/***************更新*****************/
	public String update() throws Exception {
		
		Horizontal horizontal2=(Horizontal)this.getCommonService().findByIntId(horizontal.getHorId(), "gdut.jmxywz.been.Horizontal");
		horizontal2.setHorTitle(horizontal.getHorTitle());
		horizontal2.setHorNum(horizontal.getHorNum());
		horizontal2.setHorTime(horizontal.getHorTime());
		horizontal2.setHorAccount(horizontal.getHorAccount());
		horizontal2.setHorContext(horizontal.getHorContext());
		horizontal2.setHorCopartner(horizontal.getHorCopartner());
		horizontal2.setHorPrincipal(horizontal.getHorPrincipal());
		horizontal2.setHorRec(horizontal.getHorRec());
		
		
		// TODO Auto-generated method stub
//		System.out.print("==========="+scientificInfoUpdate);
		boolean bl=this.getCommonService().updateObject(horizontal2);
//		System.out.print("==========="+scientificInfoUpdate);
//		System.out.print("==========="+bl);
		 if(upload_file!=null)
				
				for(int i=0;i<upload_file.size();i++){//控制上传部分
					String extend_name=getUpload_fileFileName().get(i);
//					System.out.println(extend_name);
					int extend_name_int=extend_name.lastIndexOf(".");//截取扩展名
					extend_name=extend_name.substring(extend_name_int);
								
					java.text.DateFormat df = new java.text.SimpleDateFormat("yyyyMMddHHmmss") ;//生成时间用于重命名文件
				   String datestr = df.format(new java.util.Date()) ;
				   Random rand = new Random();//生成随机数
				   int ram = rand.nextInt(500);
					String ram_str = String.valueOf(ram);
					for(int i_i=0; ram_str.length()<6;i_i++){
						ram_str="0"+ram_str;
					}
					

					
					
					String root = ServletActionContext.getRequest().getRealPath(
							"/accessories/horizontal/");//获得文件在服务器的绝对路径
					
					extend_name=datestr+ram_str+extend_name;//合成文件名    时间+随机数+扩展名
//					System.out.println("----文件名-:---"+extend_name+"--------------");
					 savePath=root+"\\"+extend_name;
					
//						System.out.println("----服务器保存路径-:---"+savePath+"--------------");
					 
					String fn=savePath ;
					FileOutputStream fos=new  FileOutputStream(fn);
					
					InputStream fis=new FileInputStream(upload_file.get(i));
					byte[] buffer=new byte[8192];
					int len=0;
					while((len=fis.read(buffer))>0){
					fos.write(buffer,0,len);
					
					}
					HorizontalFile afiles=new HorizontalFile();
								
					
					afiles.setHorizontalFileUrl("/accessories/horizontal/"+extend_name);
					afiles.setHorizontalFileName(upload_fileFileName.get(i));
					afiles.setHorizontalFileContext(files_content.get(i));
					afiles.setHorizontal(horizontal);	
		
					//在插入附件表
					boolean b2=this.getCommonService().saveObject(afiles);
			
//			文件上传处理结束
			
	     	}

		
		
		if(bl)
			return SUCCESS;
		else
			return "error";
	}
	
	/***************删除*****************/
	public String delete() throws Exception {
		  teacher = (Teacher)ActionContext.getContext().getSession().get(TEACHER_SESSION_KEY);

		// TODO Auto-generated method stub
		horizontal=(Horizontal)this.getCommonService().findByIntId(horId, "gdut.jmxywz.been.Horizontal");
		if(horizontal!=null){
			
			
			String hql="from HorizontalFile where horId="+horId;
	         
			Horizontal horizontal = new Horizontal();
		         
			horizontal =(Horizontal) this.getCommonService().findByIntId(horId, "gdut.jmxywz.been.Horizontal");
		   	  
		         //删除服务器上的附件
		   	  
		   	    List fileList = this.getCommonService().findListByHql(hql, null);
		   	  
		   	    Iterator iter = fileList.iterator();
		   	   
		   	    while(iter.hasNext()){
		   		  
		   	    	HorizontalFile horizontalFile = (HorizontalFile)iter.next();
		   		  
		   		  String path = ServletActionContext.getRequest().getRealPath("/");
		   		  
		   		  path = path + horizontalFile.getHorizontalFileUrl();
		   		  
		   		  System.out.println("======="+path);
		   		  
		   		  this.getCommonService().deleteObject(horizontalFile);
		   		  
		   		  this.deleteFile(path);
		   		  
		   		  System.out.println("===文件删除===");
		   	    }
		   		  
		   		boolean bl=this.getCommonService().deleteObject(horizontal);//删除科研著作
		   		
		   		if(bl)
					return SUCCESS;
				else
					return "error";
		   	  }
			
			
			
			else
				return "error";
		
	}
	
	/***************删除*****************/
	public String deleteSelf() throws Exception {
		// TODO Auto-generated method stub
		 teacher = (Teacher)ActionContext.getContext().getSession().get(TEACHER_SESSION_KEY);

		horizontal=(Horizontal)this.getCommonService().findByIntId(horId, "gdut.jmxywz.been.Horizontal");
		
		//判断是否可以删除
  	    if(!horizontal.getTeacherByEditer().getTeacherId().equals(teacher.getTeacherId())){
			
			System.out.println("没有删除的权限");
			
			return "error";
		}
		if(!"0".equals(horizontal.getAuditing())){
	   		  
	   		System.out.println("已通过审核，不能在修改！");
	   		
	   		return "error";
	   	  }
	 
		
		if(horizontal!=null){
			
			
			String hql="from HorizontalFile where horId="+horId;
	         
			Horizontal horizontal = new Horizontal();
		         
			horizontal =(Horizontal) this.getCommonService().findByIntId(horId, "gdut.jmxywz.been.Horizontal");
		   	  
		         //删除服务器上的附件
		   	  
		   	    List fileList = this.getCommonService().findListByHql(hql, null);
		   	  
		   	    Iterator iter = fileList.iterator();
		   	   
		   	    while(iter.hasNext()){
		   		  
		   	    	HorizontalFile horizontalFile = (HorizontalFile)iter.next();
		   		  
		   		  String path = ServletActionContext.getRequest().getRealPath("/");
		   		  
		   		  path = path + horizontalFile.getHorizontalFileUrl();
		   		  
		   		  System.out.println("======="+path);
		   		  
		   		  this.getCommonService().deleteObject(horizontalFile);
		   		  
		   		  this.deleteFile(path);
		   		  
		   		  System.out.println("===文件删除===");
		   	    }
		   		  
		   		boolean bl=this.getCommonService().deleteObject(horizontal);//删除科研著作
		   		
		   		if(bl)
					return SUCCESS;
				else
					return "error";
		   	  }
			
			
			
			else
				return "error";
		
	}
	
	
	
	 /***
	    *  deleteFile  从服务器上删除附件
	    * @param sPath
	    * @return
	    */
		
		public  boolean deleteFile(String sPath) {   
			boolean  flag = false;   
			File   file = new File(sPath);   
		    // 路径为文件且不为空则进行删除   
		    if (file.isFile() && file.exists()) {   
		        file.delete();   
		        System.out.println("---成功从服务器删除文件---");
		        flag = true;   
		    }   
		    return flag;   
		}  
	
	
	/***************show审核信息列表*****************/
	public String audingShow()throws Exception{
		 teacher = (Teacher)ActionContext.getContext().getSession().get(TEACHER_SESSION_KEY);

		String hql=" from Horizontal";
		 hql = hql +"  order by auditing asc ,editedTime desc,horId desc";
		pageModel=this.getCommonService().findPage(hql, null, currentPage, pageSize,"HorizontalAudingShow.action","");
//		horizontal=(Horizontal)pageModel.getList().get(0);
	
		return SUCCESS;
	}
	/***************show非管理员信息列表*****************/
	public String selfShow()throws Exception{
		 teacher = (Teacher)ActionContext.getContext().getSession().get(TEACHER_SESSION_KEY);

		String hql=" from Horizontal where teacherByEditer.teacherId="+teacher.getTeacherId();
		 hql = hql +"  order by auditing asc ,editedTime desc,horId desc";
		pageModel=this.getCommonService().findPage(hql, null, currentPage, pageSize,"HorizontalSelfShow.action","");
//		horizontal=(Horizontal)pageModel.getList().get(0);
		
		return SUCCESS;
	}
	
	
	
	/***************Search审核列表*****************/		
	public String searchAuding() throws Exception {
		// TODO Auto-generated method stub
		 teacher = (Teacher)ActionContext.getContext().getSession().get(TEACHER_SESSION_KEY);

		String hql="from Horizontal";
		
		 String[] parem = null;
   	  
   	  String  search_parameter = "";
   	  String en_where_value="";
   	  
   	 
			HttpServletRequest request = ServletActionContext.getRequest();
			where_value=new String(request.getParameter("where_value").getBytes("ISO-8859-1"),"UTF-8");
			 //
		   	  String searchValue1=where_value;
		   	  //
			//
     if(!"".equals(where_value)&&where_value!=null){
   		  
   		  en_where_value=java.net.URLEncoder.encode(where_value, "utf-8");//URL编码转换
   		  System.out.println("++++-- "+where_value);
   		  
   		  if (like.equals("like")) {
   			hql = hql + "  where " + where_search + " like '%"+ searchValue1 + "%' ";
 				//hql = hql + "  and sc." + where_search + " like '%"+ searchValue1 + "%' ";
 			} else {
 				hql = hql + "  where " + where_search + " ='"+ searchValue1 + "' ";
 				//hql = hql + "  and sc." + where_search + " ='"+ searchValue1 + "' ";
 			}
   		  
   	  }
   	  
     hql = hql +"  order by auditing asc ,editedTime desc,horId desc";
   	  
   	  //查询条件
   	  
   	  search_parameter="&where_search="+ where_search+"&like="+like+"&where_value="+en_where_value;
   	  
   	  pageModel = new PageModel();
   	  
   	  pageModel=this.getCommonService().findPage(hql, parem, currentPage, pageSize, "HorizontalSearchAuding.action",search_parameter);
		
		
		
		return SUCCESS;
	}
	
	/***************Search审核列表*****************/		
	public String searchSelf() throws Exception {
		// TODO Auto-generated method stub
		 teacher = (Teacher)ActionContext.getContext().getSession().get(TEACHER_SESSION_KEY);
		 String hql=" from Horizontal where teacherByEditer.teacherId="+teacher.getTeacherId();
		 //
		 
		 String[] parem = null;
   	  
   	  String  search_parameter = "";
   	  String en_where_value="";
   	  
   	
			HttpServletRequest request = ServletActionContext.getRequest();
			where_value=new String(request.getParameter("where_value").getBytes("ISO-8859-1"),"UTF-8");
			  //
		   	  String searchValue1=where_value;
		   	  //
			//
     if(!"".equals(where_value)&&where_value!=null){
   		  
   		  en_where_value=java.net.URLEncoder.encode(where_value, "utf-8");//URL编码转换
   		  System.out.println("++++-- "+where_value);
   		  
   		  if (like.equals("like")) {
   			hql = hql + "  and " + where_search + " like '%"+ searchValue1 + "%' ";
 				//hql = hql + "  and sc." + where_search + " like '%"+ searchValue1 + "%' ";
 			} else {
 				hql = hql + "  and " + where_search + " ='"+ searchValue1 + "' ";
 				//hql = hql + "  and sc." + where_search + " ='"+ searchValue1 + "' ";
 			}
   		  
   	  }
   	  
     hql = hql +"  order by auditing asc ,editedTime desc,horId desc";
   	  
   	  //查询条件
   	  
   	  search_parameter="&where_search="+ where_search+"&like="+like+"&where_value="+en_where_value;
   	  
   	  pageModel = new PageModel();
   	  
   	  pageModel=this.getCommonService().findPage(hql, parem, currentPage, pageSize, "HorizontalSearchSelf.action",search_parameter);
		
		
		return SUCCESS;
	}
	
	   /***
     *   inside 对内公布
     */
	  public String inside() throws Exception{
			 teacher = (Teacher)ActionContext.getContext().getSession().get(TEACHER_SESSION_KEY);

    	  
		  horizontal=(Horizontal)this.getCommonService().findByIntId(horId, "gdut.jmxywz.been.Horizontal");
    	  //将信息审核为对院内公布 ，即设置auditing为1；
		 if(horizontal!=null){
			 horizontal.setAuditing("1");
			 horizontal.setTeacherByAssessor(teacher);
		  boolean bl=this.getCommonService().updateObject(horizontal);
		   if(bl){  
		  return SUCCESS;
		   }
		   return "error";
		 }
		 return "error";
	  }
	  
	
    /***
     *   outside 对外公布
     */
    public String outside() throws Exception{
		 teacher = (Teacher)ActionContext.getContext().getSession().get(TEACHER_SESSION_KEY);

    	horizontal=(Horizontal)this.getCommonService().findByIntId(horId, "gdut.jmxywz.been.Horizontal");
    	  //将信息审核为对院内公布 ，即设置auditing为1；
		 if(horizontal!=null){
			 horizontal.setAuditing("2");
			 horizontal.setTeacherByAssessor(teacher);
		  boolean bl=this.getCommonService().updateObject(horizontal);
		   if(bl){  
		  return SUCCESS;
		   }
		   return "error";
		 }
		 return "error";
	  }
	
	/***
	 * 
	 *    notAllow 不通过
	 */
    public String notAllow() throws Exception{
		 teacher = (Teacher)ActionContext.getContext().getSession().get(TEACHER_SESSION_KEY);

    	horizontal=(Horizontal)this.getCommonService().findByIntId(horId, "gdut.jmxywz.been.Horizontal");
    	  //将信息审核为对院内公布 ，即设置auditing为1；
		 if(horizontal!=null){
			 horizontal.setAuditing("3");
			 horizontal.setTeacherByAssessor(teacher);
		  boolean bl=this.getCommonService().updateObject(horizontal);
		   if(bl){  
		  return SUCCESS;
		   }
		   return "error";
		 }
		 return "error";
	  }
	
    
    
	
	/***
	 * 
	 *    rollBack 未审核
	 */
    public String rollBack() throws Exception{
		 teacher = (Teacher)ActionContext.getContext().getSession().get(TEACHER_SESSION_KEY);

    	horizontal=(Horizontal)this.getCommonService().findByIntId(horId, "gdut.jmxywz.been.Horizontal");
    	  //将信息审核为对院内公布 ，即设置auditing为1；
		 if(horizontal!=null){
			 horizontal.setAuditing("0");
			 horizontal.setTeacherByAssessor(teacher);
		  boolean bl=this.getCommonService().updateObject(horizontal);
		   if(bl){  
		  return SUCCESS;
		   }
		   return "error";
		 }
		 return "error";
  	  }
    
	
    public String importExcel() throws Exception {
		 teacher = (Teacher)ActionContext.getContext().getSession().get(TEACHER_SESSION_KEY);
    	//System.out.print("3333333332222222222222222222222111111111113333333");
    	// TODO Auto-generated method stub
		if(excelFile==null)
		{
			return "error";
			}
				
//System.out.print("3333333333333333333333333333333");
    	int count=0;
		try{
		FileInputStream fis=new FileInputStream(excelFile);
		jxl.Workbook jwb=Workbook.getWorkbook(fis);
		int sheets=jwb.getNumberOfSheets();
		for(int i=0;i<sheets;i++){
			Sheet rs=jwb.getSheet(i);
			int columnNum=rs.getColumns();
			int rowNum=rs.getRows();
			if(rowNum>0&&columnNum!=8){
				System.out.println("--------------------导入格式不对----------------------");
				return "error";
			}
			Cell crl;
			for(int row=1;row<rowNum;row++){
				crl=rs.getCell(0, row);
				String hql="from Horizontal stc where stc.horNum='"+crl.getContents()+"'";
				List<Horizontal> aa=this.getCommonService().findListByHql(hql, null);
				
			
                 

                 boolean bl;
             
				if(aa.size()==0){//项目未存在
					
					horizontal=new Horizontal();	
					
					horizontal.setHorNum(crl.getContents());
					
					
					 crl=rs.getCell(1,row);
	                 horizontal.setHorTitle(crl.getContents());
	                 crl=rs.getCell(2,row);
	                 horizontal.setHorCopartner(crl.getContents());
	                 crl=rs.getCell(3,row);
	                 horizontal.setHorPrincipal(crl.getContents());
	                 crl=rs.getCell(4,row);
	                 horizontal.setHorAccount(Integer.valueOf(crl.getContents()).intValue());
	                 crl=rs.getCell(5,row);
	                 horizontal.setHorRec(Integer.valueOf(crl.getContents()).intValue());
	                 crl=rs.getCell(6,row);
	                
	                 
	           
	                 
	                 SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");    
	                 Date date=(Date)format1.parse(crl.getContents().toString());
	                 horizontal.setHorTime(date);
	                 
	                 
	                 crl=rs.getCell(7,row);
	                 horizontal.setHorContext(crl.getContents());
	                 horizontal.setEditedTime(new java.util.Date());
//	                 horizontal.setEditer(editer);
	                 horizontal.setAuditing("0");
	                 horizontal.setTeacherByEditer(teacher);
					bl=this.getCommonService().saveObject(horizontal);//不存在该项目

					if(!bl){
						System.out.println("------------------存入数据库出错-------------------");
					}
					
					count++;
					
				}
				
				if(aa.size()!=0){//项目已存在
						
					
					
					if(checkbox&&aa.get(0).getAuditing().equals("0")){//判断是否对已存在项目进行更新
						//对已存在项目进行更新	
						
						horizontal=aa.get(0);
						horizontal.setHorNum(crl.getContents());
						 crl=rs.getCell(1,row);
		                 horizontal.setHorTitle(crl.getContents());
		                 crl=rs.getCell(2,row);
		                 horizontal.setHorCopartner(crl.getContents());
		                 crl=rs.getCell(3,row);
		                 horizontal.setHorPrincipal(crl.getContents());
		                 crl=rs.getCell(4,row);
		                 horizontal.setHorAccount(Integer.valueOf(crl.getContents()).intValue());
		                 crl=rs.getCell(5,row);
		                 horizontal.setHorRec(Integer.valueOf(crl.getContents()).intValue());
		                 crl=rs.getCell(6,row);
		               horizontal.setHorTime(new SimpleDateFormat("yyyy-MM-dd").parse(crl.getContents()));
		                 crl=rs.getCell(7,row);
		                 horizontal.setHorContext(crl.getContents());
		                 horizontal.setEditedTime(new java.util.Date());
//		                 horizontal.setEditer(editer);
		                 horizontal.setAuditing("0");
		                 horizontal.setTeacherByEditer(teacher);
						bl=this.getCommonService().updateObject(horizontal);//对已存在该项目更新
					
						if(!bl){
							System.out.println("------------------存入数据库出错-------------------");
						}
						
						count++;
					}
					
					else{
						System.out.print("项目已存在，但不做更新");//不做任何更新
					}
					
				}
				
				
				
				
			}
		}
		System.out.println("=================一共导入数据数：================="+count);
		jwb.close();
		fis.close();
		}catch(Exception e){
			e.printStackTrace();
		}
		return SUCCESS;
	}
	
    //管理员导出Excel
    public void exportExcel() throws Exception {
    	
    	
    	// TODO Auto-generated method stub
		try{
			String[] str=new String[]{"项目编号","项目名称","合作单位","负责人","项目资金","来款情况","项目时间","备注","发布人","发布时间","审核状态（0==未审核，1==院内公开，2==对外公开,3==审核不通过）","审核信息"};
			List list;
			
				String hql="from Horizontal";
				list=this.getCommonService().findListByHql(hql, null);
			
			
			
			HttpServletResponse response=ServletActionContext.getResponse();
			response.reset();
			response.setContentType("application/vnd.ms-excel");
			response.setContentType("APPLICATION/OCTET-STREAM ");
			response.setHeader("Content-Disposition","attachment; filename="+java.net.URLEncoder.encode("横向项目.xls","UTF-8"));
			WritableWorkbook wwb=Workbook.createWorkbook(response.getOutputStream());
			WritableSheet sheet=wwb.createSheet("sheet1", 0);
			sheet.setColumnView(0, 20);
			sheet.setColumnView(1, 20);
			sheet.setColumnView(2, 20);
			sheet.setColumnView(3, 20);
			sheet.setColumnView(11, 20);
			Label label=null;
			for(int i=0;i<str.length;i++){
				label=new Label(i,0,str[i]);
				sheet.addCell(label);
			}
			for(int i=0;i<list.size();i++){
				horizontal=(Horizontal)list.get(i);
				
				label = new Label(0, i+1, horizontal.getHorNum());
				sheet.addCell(label);
				label = new Label(1, i+1, horizontal.getHorTitle());
				sheet.addCell(label);
				label = new Label(2, i+1, horizontal.getHorCopartner());
				sheet.addCell(label);
				label = new Label(3, i+1, horizontal.getHorPrincipal());
				sheet.addCell(label);
				label = new Label(4, i+1, String.valueOf(horizontal.getHorAccount()));
				sheet.addCell(label);
				label = new Label(5, i+1, String.valueOf(horizontal.getHorRec()));
				sheet.addCell(label);
				label = new Label(6, i+1,  new SimpleDateFormat("yyyy-MM-dd").format(horizontal.getHorTime()));
				sheet.addCell(label);
				label = new Label(7, i+1, horizontal.getHorContext());
				sheet.addCell(label);
				label = new Label(8, i+1, horizontal.getTeacherByEditer().getTeacherName());
				sheet.addCell(label);
				label = new Label(9, i+1,new SimpleDateFormat("yyyy-MM-dd").format(horizontal.getEditedTime()));
				sheet.addCell(label);
				label = new Label(10, i+1, horizontal.getAuditing());
				sheet.addCell(label);
//				label = new Label(11, i+1, horizontal.getAssessor().getTeacherName());
//				sheet.addCell(label);
				label = new Label(11, i+1, horizontal.getAssessorInfo());
				sheet.addCell(label);
		
			}
			wwb.write();
			wwb.close();
			
		}catch(Exception e){
			e.printStackTrace();
		}
		
	}
    
    //非管理员导出Excel
 public void exportSelfExcel() throws Exception {
	 teacher = (Teacher)ActionContext.getContext().getSession().get(TEACHER_SESSION_KEY);


		String hql=" from Horizontal where teacherByEditer.teacherId="+teacher.getTeacherId();
    	
    	// TODO Auto-generated method stub
		try{
			String[] str=new String[]{"项目编号","项目名称","合作单位","负责人","项目资金","来款情况","项目时间","备注","发布人","发布时间","审核状态（0==未审核，1==院内公开，2==对外公开,3==审核不通过）","审核信息"};
			List list;
			
			
				list=this.getCommonService().findListByHql(hql, null);
			
			
		
			
			HttpServletResponse response=ServletActionContext.getResponse();
			response.reset();
			response.setContentType("application/vnd.ms-excel");
			response.setContentType("APPLICATION/OCTET-STREAM ");
			response.setHeader("Content-Disposition","attachment; filename="+java.net.URLEncoder.encode("横向项目.xls","UTF-8"));
			WritableWorkbook wwb=Workbook.createWorkbook(response.getOutputStream());
			WritableSheet sheet=wwb.createSheet("sheet1", 0);
			sheet.setColumnView(0, 20);
			sheet.setColumnView(1, 20);
			sheet.setColumnView(2, 20);
			sheet.setColumnView(3, 20);
			sheet.setColumnView(11, 20);
			Label label=null;
			for(int i=0;i<str.length;i++){
				label=new Label(i,0,str[i]);
				sheet.addCell(label);
			}
			for(int i=0;i<list.size();i++){
				horizontal=(Horizontal)list.get(i);
				
				label = new Label(0, i+1, horizontal.getHorNum());
				sheet.addCell(label);
				label = new Label(1, i+1, horizontal.getHorTitle());
				sheet.addCell(label);
				label = new Label(2, i+1, horizontal.getHorCopartner());
				sheet.addCell(label);
				label = new Label(3, i+1, horizontal.getHorPrincipal());
				sheet.addCell(label);
				label = new Label(4, i+1, String.valueOf(horizontal.getHorAccount()));
				sheet.addCell(label);
				label = new Label(5, i+1, String.valueOf(horizontal.getHorRec()));
				sheet.addCell(label);
				label = new Label(6, i+1,  new SimpleDateFormat("yyyy-MM-dd").format(horizontal.getHorTime()));
				sheet.addCell(label);
				label = new Label(7, i+1, horizontal.getHorContext());
				sheet.addCell(label);
				label = new Label(8, i+1, horizontal.getTeacherByEditer().getTeacherName());
				sheet.addCell(label);
				label = new Label(9, i+1,new SimpleDateFormat("yyyy-MM-dd").format(horizontal.getEditedTime()));
				sheet.addCell(label);
				label = new Label(10, i+1, horizontal.getAuditing());
				sheet.addCell(label);
//				label = new Label(11, i+1, horizontal.getAssessor().getTeacherName());
//				sheet.addCell(label);
				label = new Label(11, i+1, horizontal.getAssessorInfo());
				sheet.addCell(label);
		
			}
			wwb.write();
			wwb.close();
			
		}catch(Exception e){
			e.printStackTrace();
		}
		
	}
    
	
	
	
	public Horizontal getHorizontal() {
		return horizontal;
	}

	public void setHorizontal(Horizontal horizontal) {
		this.horizontal = horizontal;
	}

	public PageModel getPageModel() {
		return pageModel;
	}

	public void setPageModel(PageModel pageModel) {
		this.pageModel = pageModel;
	}

	public int getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
/*
	public String getSearchSelect() {
		return searchSelect;
	}

	public void setSearchSelect(String searchSelect) {
		this.searchSelect = searchSelect;
	}*/

	public String getLike() {
		return like;
	}

	public void setLike(String like) {
		this.like = like;
	}

	/*public String getSearchValue() {
		return searchValue;
	}

	public void setSearchValue(String searchValue) {
		this.searchValue = searchValue;
	}*/

	public int getHorId() {
		return horId;
	}

	public void setHorId(int horId) {
		this.horId = horId;
	}


	public List<String> getFiles_content() {
		return files_content;
	}


	public void setFiles_content(List<String> files_content) {
		this.files_content = files_content;
	}


	public List<File> getUpload_file() {
		return upload_file;
	}


	public void setUpload_file(List<File> upload_file) {
		this.upload_file = upload_file;
	}


	public List<String> getUpload_fileContentType() {
		return upload_fileContentType;
	}


	public void setUpload_fileContentType(List<String> upload_fileContentType) {
		this.upload_fileContentType = upload_fileContentType;
	}


	public List<String> getUpload_fileFileName() {
		return upload_fileFileName;
	}


	public void setUpload_fileFileName(List<String> upload_fileFileName) {
		this.upload_fileFileName = upload_fileFileName;
	}


	public String getSavePath() {
		return savePath;
	}


	public void setSavePath(String savePath) {
		this.savePath = savePath;
	}


	public List<HorizontalFile> getHorizontalFile() {
		return horizontalFile;
	}


	public void setHorizontalFile(List<HorizontalFile> horizontalFile) {
		this.horizontalFile = horizontalFile;
	}


	public File getExcelFile() {
		return excelFile;
	}


	public void setExcelFile(File excelFile) {
		this.excelFile = excelFile;
	}

	public String getExcelFileContentType() {
		return excelFileContentType;
	}




	public void setExcelFileContentType(String excelFileContentType) {
		this.excelFileContentType = excelFileContentType;
	}




	public boolean isCheckbox() {
		return checkbox;
	}

	public void setCheckbox(boolean checkbox) {
		this.checkbox = checkbox;
	}




	public Teacher getTeacher() {
		return teacher;
	}




	public void setTeacher(Teacher teacher) {
		this.teacher = teacher;
	}




	public String getWhere_search() {
		return where_search;
	}




	public void setWhere_search(String whereSearch) {
		where_search = whereSearch;
	}




	public String getWhere_value() {
		return where_value;
	}




	public void setWhere_value(String whereValue) {
		where_value = whereValue;
	}


	
	
	
	
	
	
	
}
